# Maintainer: James Duley <jagduley gmail>
# Maintainer: Philippe Renon <philippe_renon yahoo.fr>

_realname=osgearth
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.3
pkgrel=7
arch=('any')
pkgdesc="A terrain rendering toolkit for OpenSceneGraph (mingw-w64)"
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
pkgdesc="A terrain rendering toolkit for OpenSceneGraph (mingw-w64)"
license=('spdx:LGPL-3.0-or-later WITH LGPL-3.0-linking-exception')
url="http://osgearth.org"
depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-geos"
        #  "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-leveldb"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libzip"
         "${MINGW_PACKAGE_PREFIX}-poco"
         "${MINGW_PACKAGE_PREFIX}-protobuf"
         "${MINGW_PACKAGE_PREFIX}-rocksdb"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph"
         "${MINGW_PACKAGE_PREFIX}-osgQt")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "git")
_tag=${_realname}-${pkgver}
source=("${_realname}"::"git+https://github.com/gwaldron/osgearth.git#tag=${_tag}"
        "0001-calling-convention.patch"
        "0002-cxx-standard-17.patch")
sha256sums=('SKIP'
            'a17f6e6160b2b399a136090c6b714baeddf41bb3b92c804d571b329e19a32063'
            '0e47076137aa18952679037e76116dc039385bea2bbbe387d1605a0462513004')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}"

  apply_patch_with_msg \
    0001-calling-convention.patch \
    0002-cxx-standard-17.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CXXFLAGS+=" -w -Wno-register" \
  FREETYPE_DIR=${MINGW_PREFIX} \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    -DOSG_DIR="${MINGW_PREFIX}" \
    -DOSGEARTH_BUILD_EXAMPLES=OFF \
    -DOSGEARTH_BUILD_TESTS=OFF \
    -DOSGEARTH_BUILD_LEGACY_SPLAT_NODEKIT=ON \
    -DOSGEARTH_BUILD_LEVELDB_CACHE=ON \
    -DOSGEARTH_BUILD_ZIP_PLUGIN=ON \
    -DPROTOBUF_USE_DLLS=ON \
    ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd build-${MSYSTEM}
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  mv "${pkgdir}${MINGW_PREFIX}"/cmake "${pkgdir}${MINGW_PREFIX}"/lib/cmake

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/osgearth
  cp -rf "${srcdir}"/${_realname}/data "${pkgdir}${MINGW_PREFIX}"/share/osgearth

  install -Dm644 "${srcdir}/${_realname}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
